package com.xinqi.modules.sys.dict.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.sys.dict.convert.SchoolConvert;
import com.xinqi.modules.sys.dict.domain.SchoolEntity;
import com.xinqi.modules.sys.dict.dto.req.SchoolCreateDTO;
import com.xinqi.modules.sys.dict.dto.req.SchoolDeleteDTO;
import com.xinqi.modules.sys.dict.dto.req.SchoolQueryDTO;
import com.xinqi.modules.sys.dict.dto.req.SchoolUpdateDTO;
import com.xinqi.modules.sys.dict.dto.rsp.SchoolResultDTO;
import com.xinqi.modules.sys.dict.manager.SchoolManager;
import com.xinqi.modules.sys.dict.service.SchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 学校表服务层实现
 *
 * @author: wenjie
 * @date: 2023/05/19
 */
@Validated
@Service("schoolService")
@RequiredArgsConstructor
public class SchoolServiceImpl implements SchoolService {
    private final SchoolConvert schoolConvert;
    private final SchoolManager schoolManager;

    /**
     * 分页查询 学校表数据
     */
    @Override
    @DS("slaver")
    public PageInfo<SchoolResultDTO> page(Param pageable, SchoolQueryDTO query) {
        IPage<SchoolEntity> page = schoolManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, schoolConvert::convert);
    }

    /**
     * 查询 学校表数据
     */
    @Override
    public List<SchoolResultDTO> find(SchoolQueryDTO query) {
        return schoolManager.list(wrapper(query)).stream()
            .map(schoolConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<SchoolEntity> wrapper(SchoolQueryDTO query) {
        LambdaQueryWrapper<SchoolEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(SchoolEntity::getId, v));
        Queries.accept(query.getCode(), v -> wrapper.eq(SchoolEntity::getCode, v));
        Queries.accept(query.getProvince(), v -> wrapper.eq(SchoolEntity::getProvince, v));
        Queries.accept(query.getCity(), v -> wrapper.eq(SchoolEntity::getCity, v));
        Queries.accept(query.getArea(), v -> wrapper.eq(SchoolEntity::getArea, v));
        Queries.accept(query.getName(), v -> wrapper.like(SchoolEntity::getName, v));
        Queries.accept(query.getEnglishName(), v -> wrapper.like(SchoolEntity::getEnglishName, v));
        return wrapper;
    }

    /**
     * 根据id查询 学校表数据
     */
    @Override
    public SchoolResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(schoolManager.findById(id)).map(schoolConvert::convert).orElse(null);
    }

    /**
     * 新增 学校表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(@Valid SchoolCreateDTO dto) {
        SchoolEntity entity = schoolConvert.create(dto);
        schoolManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 学校表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@Valid SchoolUpdateDTO dto) {
        Optional.ofNullable(schoolManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        SchoolEntity entity = schoolConvert.update(dto);
        return schoolManager.updateById(entity);
    }

    /**
     * 根据id删除 学校表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return schoolManager.removeById(id);
    }

    /**
     * 根据id删除 学校表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(SchoolDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
